﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using System.Web;
using System.Web.Security;

namespace One.Framework.UI.Security
{
    public class Principal : GenericPrincipal
    {
        /// <summary>
        /// 인증여부확인
        /// </summary>
        public static bool IsAuthenticated
        {
            get
            {
                if (HttpContext.Current.User != null)
                {
                    if (HttpContext.Current.User.Identity.IsAuthenticated)
                    {
                        if (HttpContext.Current.User.Identity is FormsIdentity)
                            return true;
                    }
                }

                return false;
            }
        }

        /// <summary>
        /// 인증 사용자 아이디
        /// </summary>
        public static string UserID
        {
            get
            {
                if (Principal.IsAuthenticated)
                {
                    return GetUserData() != null ? GetUserData()[0] : string.Empty;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 로그인 사용자 아이디
        /// </summary>
        public static string LoginUserID
        {
            get
            {
                if (Principal.IsAuthenticated)
                {
                    FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;

                    return id.Name;
                }
                else
                {
                    return string.Empty;
                }
            }
        }

        /// <summary>
        /// 사용자명 추출
        /// </summary>
        public static string UserName
        {
            get
            {

                if (Principal.IsAuthenticated)
                {
                    return GetUserData() != null ? GetUserData()[1] : string.Empty;
                }
                else
                {
                    return string.Empty;
                }

            }
        }

        /// <summary>
        /// 사용자그룹 아이디 배열 추출
        /// </summary>
        public static int[] GroupId
        {
            get
            {
                int[] iGroupId;

                if (Principal.IsAuthenticated)
                {
                    string[] sGroupId = GetUserData()[2].Split(',');
                    iGroupId = new int[sGroupId.Length];

                    for (int i = 0; i < sGroupId.Length; i++)
                    {
                        iGroupId[i] = int.Parse(sGroupId[i]);
                    }
                    return iGroupId;

                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 권한그룹아이디 배열추출/
        /// </summary>
        public static int[] RoleId
        {
            get
            {
                int[] iRoleId;

                if (Principal.IsAuthenticated)
                {
                    if (GetUserData() != null)
                    {
                        string[] sRoleId = GetUserData()[3].Split(',');
                        iRoleId = new int[sRoleId.Length];

                        for (int i = 0; i < sRoleId.Length; i++)
                        {
                            iRoleId[i] = int.Parse(sRoleId[i]);
                        }
                        return iRoleId;
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 권한그룹명 배열추출
        /// </summary>
        public static string[] RoleName
        {
            get
            {
                if (Principal.IsAuthenticated)
                {
                    return GetUserData() != null ? GetUserData()[4].Split(',') : null;
                }
                else
                {
                    return null;
                }
            }
        }

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="identity"></param>
        /// <param name="sArrRole"></param>
        public Principal(IIdentity identity, string[] sArrRole)
            : base(identity, sArrRole)
        {
        }

        /// <summary>
        /// 권한그룹 인증쿠키 배열 추출함수
        /// </summary>
        /// <returns></returns>
        public static string[] GetUserData()
        {
            FormsIdentity id = (FormsIdentity)HttpContext.Current.User.Identity;
            FormsAuthenticationTicket ticket = id.Ticket;

            // Get the stored user-data, in this case, our roles (권한그룹ID)
            string userData = ticket.UserData;
            if (userData.IndexOf(":") != -1)
            {
                string[] arrUserData = userData.Split(':');
                return arrUserData;
            }

            return null;
        }
    }
}
